package com.byq.mm.abstractx.cluster.client;

import com.byq.mm.abstractx.device.Device;
import com.byq.mm.abstractx.engine.CoreEnvironment;
import com.byq.mm.abstractx.protocol.Defination;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;



public class ClientDeviceManager {
	public ClientDeviceManager(CoreEnvironment env) {
		super();
		this.env = env;
		Collection<Device> ds = env.getLiteStore().findRootDevices();
		if(ds == null)
			return;
		for(Device d : ds){
			put(d);
		}
	}
	void remove(String transportId){
		DeviceClient d = devices.remove(transportId);
		if(d != null){
			demap(d.getDevice().getGlobalId());
		}
		
	}
	void demap(String globalId){
		DeviceClient dc = map.remove(globalId);
		for(Device c : dc.getDevice().getSubdevices())
			demap(c.getGlobalId());
	}


	CoreEnvironment env;
	Map<String, DeviceClient> devices = new HashMap<String, DeviceClient>();
	Map<String, DeviceClient> map = new HashMap<String, DeviceClient>();
//	Set<String> whiteList = new HashSet<String>();
//	Map<String, Boolean> permitted = new HashMap<String,  Boolean>();

	public void accept(Defination d) {
		Device dev = d.getDevice();
		deledeDefination(dev.getTransportId());
		put(dev);
		env.getLiteStore().saveDevice(dev);
		sendAccept(dev.getTransportId());
		env.getEventCenter().notifyDeviceChange();
	}
	public void removeDefination(String transport){
		deledeDefination(transport);
		env.getEventCenter().notifyDeviceChange();
	}
	
	private void deledeDefination(String transport){
		Device dev = getRootDevice(transport);
		if(dev != null){
		
		remove(transport);
		env.getLiteStore().deleteDevice(dev);
		}
		
	}
	void sendAccept(String id) {
		
		Defination dd = new Defination();
		dd.accept(null);
		env.getExtensionManager().sendDefination(dd, id);
	}
	public void reject(Defination d) {
		Device dev = d.getDevice();
		sendReject(dev.getTransportId());
	}
	public void refuse(String jid) {

		sendRefuse(jid);
	}
	void sendReject(String id) {
			
			Defination dd = new Defination();
			dd.reject();
			env.getExtensionManager().sendDefination(dd, id);
	}
	void sendRefuse(String id) {
	
		Defination dd = new Defination();
		dd.refuse();
		env.getExtensionManager().sendDefination(dd, id);
}
	public void acceptAndDefine(Defination d) {
		Device dev = d.getDevice();
		
		put(dev);
		env.getServerDeviceManager().sendAcceptAndDefine(dev.getTransportId());
		env.getEventCenter().notifyDeviceChange();
	}
	private void put(Device d) {
		DeviceClient dev = map(d);
		devices.put(dev.getDevice().getTransportId(), dev);
		
	}
	DeviceClient map(Device d){
		DeviceClient dev = new DeviceClient(d, env);
		map.put(dev.getDevice().getGlobalId(), dev);
		for(Device c : d.getSubdevices()){
			map(c);
		}
		return dev;
	}

	public DeviceClient getDeviceClient(String path) {
		// TODO Auto-generated method stub
		return map.get(path);
	}
	public Collection<DeviceClient> getDevices(){
		return devices.values();
	}
	public Device getRootDevice(String transport){
		DeviceClient c = devices.get(transport);
		if(c == null)
			return null;
		return c.getDevice();
	}
//	public void interact(Interaction i, String to) {
//		final Message m =  new Message(to, Message.Type.chat);
//		InteractionExtension pe = new InteractionExtension();
//
//		pe.setInteraction(i);
//		m.addExtension(pe);
//		new Thread() {
//			@Override
//			public void run() {
//				if (env.getConnection() != null && env.getConnection().getUser() != null) {
//					env.getConnection().sendPacket(m);
//					
//				}
//			}
//		}.start();
//		
//	}
}
